<?php
include 'lib_conf.php';
include 'lib/tbl_namhoa_user.php';

if ($s_Action == 'download-image') {
    
    // <editor-fold defaultstate="collapsed" desc="Download_Avartar">
    $db_query  = "select userid from user where userid = '$f_userid' limit 1";
    $db_result = db_query($db_conn, $db_query);
    
    if (!($d_user = db_fetch($db_result))) { raise_error('Select One'); }
    
    $f_size = isset($_REQUEST['size']) ? '-' . $_REQUEST['size'] : '';

    $v_filename = $f_userid . $f_size . '.png';
    
    if (!file_exists($s_User_Path . $v_filename)) { $v_filename = 'no-image' . $f_size . '.png'; }
    if (!file_exists($s_User_Path . $v_filename)) { $v_filename = 'no-image.png'; }

    $v_filesize     = filesize($s_User_Path . $v_filename);
    $v_speed        = 1024  ;
    $v_seekstart    = 0     ;

    header('Content-Disposition: attachment; filename="' . $v_filename . '"');
    header('Content-Transfer-Encoding: binary'                              );
    header('Accept-Rangess: bytes'                                          );
    header('Content-Length: ' . intval($v_filesize - $v_seekstart)          );
    header('Connection: close'                                              );
    header('Content-Type: application/octet-stream'                         );
    
    $fp = fopen($s_User_Path . $v_filename, 'rb');
    
    fseek($fp, $v_seekstart);
    
    while (!feof($fp)) {
        set_time_limit(0);
        echo fread($fp, round(1024 * $v_speed));
        flush() ;
    }
    fclose($fp);
    // </editor-fold>
}
else {
    raise_error('Bạn chưa được cấp quyền thực hiện chức năng này');
}
?>
